package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台管理中心
 *
 * @author ZYB
 * @since 2021-05-11
 */
@Api("后台管理中心")
@RestController
@RequestMapping("/admin/product")
//@CrossOrigin//网关中已跨域
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    /*@Autowired
    private ListFeignClient listFeignClient;*/

    @ApiOperation("获得一级目录")
    @GetMapping("/getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> baseCategory1List = managerService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    @ApiOperation("获得二级目录")
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable  Long category1Id){
        List<BaseCategory2> baseCategory2List = managerService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    @ApiOperation("获得三级目录")
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable  Long category2Id){
        List<BaseCategory3> baseCategory3List = managerService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    @ApiOperation("根据一二三级分类的id 层级查询 平台属性以及属性值表")
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable  Long category1Id,
                               @PathVariable  Long category2Id,
                               @PathVariable  Long category3Id){
        List<BaseAttrInfo> baseAttrInfoList = managerService.attrInfoList(category1Id,
                category2Id,category3Id);
        return Result.ok(baseAttrInfoList);
    }

    @ApiOperation("保存属性值名称")
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo (@RequestBody BaseAttrInfo baseAttrInfo){
        managerService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }


    @ApiOperation("查询品牌列表 分页")
    @GetMapping("baseTrademark/{page}/{limit}")
    public Result baseTrademark(@PathVariable Integer page,@PathVariable Integer limit){
        //分页对象的接口
        IPage iPage = managerService.baseTrademark(page,limit);

        return Result.ok(iPage);
    }

    @ApiOperation("获取SPU列表 分页")
    @GetMapping("/{page}/{limit}")
    public Result spuInfoList(@PathVariable Integer page,@PathVariable Integer limit,Long category3Id){
        //分页对象的接口
        IPage iPage = managerService.spuInfoList(page,limit,category3Id);

        return Result.ok(iPage);
    }

    @ApiOperation("获取品牌属性")
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList = managerService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    @ApiOperation("获取销售属性")
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = managerService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    @ApiOperation("保存SPU")
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        managerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    @ApiOperation("根据SPUId查询所有图片")
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){
        List<SpuImage> spuImageList = managerService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }



    @ApiOperation("根据SPUId查询销售属性以及属性值")
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrs = managerService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrs);
    }

    @ApiOperation("添加库存")
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    @ApiOperation("SKU分页查询")
    @GetMapping("list/{page}/{limit}")
    public Result skuListPage(@PathVariable Integer page,@PathVariable Integer limit){
        IPage iPage = managerService.skuListPage(page,limit);
        return Result.ok(iPage);
    }

    @ApiOperation("上架")
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        managerService.onSale(skuId);
        //远程调用添加索引库
        //listFeignClient.onSale(skuId);

        return Result.ok();
    }

    @ApiOperation("下架")
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        managerService.cancelSale(skuId);
        //远程删除索引库
        //listFeignClient.cancelSale(skuId);
        return Result.ok();
    }







}

